//-----------------------------------------------------------------------
// <copyright file="HighlightGroup.cs" company="ExcelTechnology Inc">
//                           ExcelTechnology Inc.
// This program is protected by copyright law and by international
// conventions. All licensing, renting, lending or copying (including
// for private use), and all other use of the program, which is not
// expressively permitted by ExcelTechnology Inc., is a violation of the rights
// of ExcelTechnology Inc.. Such violations will be reported to the
// appropriate authorities.
// Violation of any copyright is punishable by law and can lead to 
// imprisonment and liability to pay damages
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor;

namespace SimianReshaperUI
{
	public class HighlightGroup : IDisposable
	{
		#region fields
		List<TextMarker> markers = new List<TextMarker>();
		TextEditorControl editor;
		IDocument document;
		#endregion

		#region properties
		public IList<TextMarker> Markers 
		{ 
			get 
			{ 
				return markers.AsReadOnly(); 
			} 
		}
		#endregion

		#region constructors
		public HighlightGroup(TextEditorControl editor)
		{
			this.editor = editor;
			document = editor.ActiveTextAreaControl.TextArea.Document;
		}
		#endregion

		#region public methods

		public void AddMarker(TextMarker marker)
		{
			markers.Add(marker);
			document.MarkerStrategy.AddMarker(marker);
		}

		public void ClearMarkers()
		{
			foreach (TextMarker m in markers)
			{
				document.MarkerStrategy.RemoveMarker(m);
			}
			if (markers != null)
			{
				markers.Clear();
			}
			if (editor != null)
			{
				editor.Refresh();
			}
		}
		#endregion

		#region IDisposible members
//		public void Dispose() 
//		{ 
//			ClearMarkers(); 
//			GC.SuppressFinalize(this); 
//		}
//
//		~HighlightGroup() 
//		{ 
//			Dispose();
//		}
		#endregion
		
		 #region Idisposible Members2
         private bool isDisposed;

        ~HighlightGroup()
         {
           Dispose(false);
         }
       
         protected virtual void Dispose(bool disposing)
         {
             if (!isDisposed)
             {
                 if (disposing)// Release managed resources
                 {
                 	ClearMarkers();
                    //this.Dispose();
                 }
                 // Release Unmanaged resources
             }
             isDisposed = true;
         }
       
         public void Dispose()
         {
           Dispose(true);
           GC.SuppressFinalize(this);
         }

        #endregion
	}
}
